<!DOCTYPE html>
<html>

<head>
    <meta charset=utf-8>
    <meta name="viewport" content="initial-scale=1.0,maximum-scale=1,user-scalable=no" />
    <meta name="format-detection" content="telephone=no" />
    <title>升级更新</title>
    <style>
        body,
        html {
            margin: 0;
            background: none;
            display: block;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }

        .fixed {
            display: none;
            margin-left: -140px;
            margin-top: -140px;
            position: fixed;
            top: 40%;
            left: 50%;
            width: 280px;
            background-color: #FFF;
            overflow: hidden;
            border-radius: 12px;
            box-shadow: 0 1px 10px rgba(0, 0, 0, 0.1);
        }

        .btn {
            position: relative;
            height: 42px;
            line-height: 42px;
            background-color: #4C78FF;
            text-align: center;
            font-size: 16px;
            color: #FFF;
            border-radius: 24px;
            box-shadow: 0px 5px 10px 0px rgba(74, 120, 255, 0.2);
            overflow: hidden;
            letter-spacing: 2px;
        }

        .process {
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            background-color: #51C332;
            text-align: center;
            color: #FFF;
        }

        #state {
            position: relative;
            text-align: center;
            font-size: 14px;
        }
    </style>
</head>

<body>
    <div class="fixed" id="Tip">
        <img src="./static/img/huojian.png" alt="升级" style="width:100%">
        <div style="font-size: 20px;color:#333;padding-left: 26px" id="newVersion">新版本更新</div>
        <div style="padding:12px 26px;">
            <div style="font-size:14px;max-height:154px;overflow-x:hidden;overflow-y: auto ">

                <div style="font-size:14px;color:#999" id="Version"></div>
                <div style="font-size: 14px;color:#333">更新内容</div>
                <div style="white-space:pre-wrap;word-break: break-all;min-height:60px;font-size:14px;line-height: 20px;"
                    id="Descn"></div>
            </div>
            <div style="padding-top:12px">
                <div class="btn" id="Btn">
                    <div class="process" id="process"></div>
                    <div id="state">立即更新</div>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    var MiniProgram = undefined
    var page = {
        lastVersion:'',
        newVersion:'',
        newContent:'',
        newURL:'',
        isNeedInstall:false,
        downloading: false,
        start: function (w, text) {
            w = (w || 0).toFixed(0)
            var PC = document.getElementById('state')
            var pr = document.getElementById('process')
            pr.style.width = w + '%'
            PC.innerText = text || '100%'
        },
        install: function () {
            console.log(111)
            if (page.downloading) { return }
            page.downloading = true
            page.start(0, '准备更新')
            if (page.isNeedInstall) {
                //需要重新安装
                if (api.systemType == "android") {
                    api.download({ 
                        url: page.newURL,
                        savePath:'fs://download/'+page.newVersion+'.apk',
                        report: true,
                        cache: false,
                        allowResume:false
                    }, function (ret, err) {
                        if (ret && 0 == ret.state) {
                            page.start(ret.percent, '下载 ' + Math.floor(ret.percent) + '%')
                        } else if (ret && 1 == ret.state) {
                            page.start(0, '开始安装')
                            api.installApp({ appUri: ret.savePath })
                            page.downloading = false
                        }
                    })
                } else if (api.systemType == "ios") {
                    page.start(0, '立即更新')
                    api.installApp({ appUri: page.newURL })
                    page.downloading = false
                }
            } else {
                //云修复
                var SavePath = 'box://'+page.newVersion+'.zip'
                var ZipPath = 'box://'+page.newVersion+'/'
                var LastPath= page.lastVersion?('box://'+page.lastVersion):''
                //下载更新包
                api.download({
                    url: page.newURL,
                    savePath: SavePath,
                    report: true,
                    cache: false,
                    allowResume: true
                }, function (ret) {
                    if (ret.state == 1) {
                        page.start(100, '下载完成，正在解压')
                        var zip = api.require('zip')
                        zip.unarchive({
                            file: SavePath,
                            password: '',
                            toPath: ZipPath 
                        }, function (jieya) {
                            if (jieya.status) {                              
                                var fsys=api.require('fs')                                
                                //删除解压包
                                fsys.exist({ path: SavePath }, function (fret) {
                                    if (fret.exist) {
                                        fsys.remove({ path: SavePath })
                                    }
                                })
                                //删除旧版本文件       
                                if(LastPath){                         
                                    fsys.exist({ path: LastPath }, function (fret) {
                                        if (fret.exist&&fret.directory) {
                                            fsys.rmdir({ path: LastPath })
                                        }
                                    })
                                }
                                api.alert({
                                    title: '更新成功',
                                    msg: '应用更新完毕，点击确定后将重新启动。',
                                }, function () {
                                    api.rebootApp()
                                })
                            } else {
                                api.alert({
                                    title: '更新失败',
                                    msg: jieya.msg,
                                }, function () { })
                                page.start(100, '再试一次')
                                page.downloading = false
                            }
                        })
                        return
                    } else if (ret.state == 2) {
                        api.alert({
                            title: '更新失败',
                            msg: ret.msg
                        })
                        page.start(100, '再试一次')
                        page.downloading = false
                    } else if (ret.state == 0) {
                        page.start(ret.percent, '下载 ' + Math.floor(ret.percent) + '%')
                    }
                })               
            }
        }
    }


    apiready = function () {
   
        page.lastVersion=api.pageParam.lastVersion
        page.newVersion=api.pageParam.newVersion
        page.newContent=api.pageParam.descn
        page.newURL=api.pageParam.url
        page.isNeedInstall=api.pageParam.isNeedReInstall


        document.getElementById('Version').innerText = "最新版本 " + page.newVersion
        document.getElementById('Descn').innerText =page.newContent
        
        if (page.isNeedInstall) {
            page.start(0, '下载新版本')
            document.getElementById('newVersion').innerText = "有新版本更新啦！"
        } else {
            page.start(0, '立即更新')
            document.getElementById('newVersion').innerText = "有新内容发布啦！"
        }

        document.getElementById('Btn').addEventListener('click', function () {
            if (api.systemType == 'android') {
                api.requestPermission({ list: ['storage'] }, function (ret) {
                    if(ret.list[0].granted){
                        page.install()
                    }
                })
            } else {
                page.install()
            }
        })

        document.getElementById('Tip').style.display = 'block'
    }
</script>

</html>